<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd">添加地铁</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">省/市：</span>
        <elui-china-area-dht :leave="2" v-model="searchRegion" @change="handleSearchRegionChange" clearable></elui-china-area-dht>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">X号线：</span>
        <el-input placeholder="请输入X号线" v-model="searchParams.subway_no" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
<!--      <div class="task-one__item">-->
<!--        <span class="task-one__label">站点名称：</span>-->
<!--        <el-input placeholder="请输入站点名称" v-model="searchParams.station_title" @keyup.enter.native="handleSearch"-->
<!--                  clearable></el-input>-->
<!--      </div>-->

      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="province" label="省"> </el-table-column>
      <el-table-column prop="city" label="市"> </el-table-column>
      <el-table-column prop="subway_no" label="X号线"> </el-table-column>
      <el-table-column prop="is_stop" label="是否停用">
        <template #default="{ row }">
          <el-text v-if="row.is_stop==1" type="info">停用</el-text>
          <el-text v-if="row.is_stop==2" type="success">正常</el-text>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{subway_id:row.subway_id})">编辑</el-button>
          <el-button link type="primary" @click="handleStation(row)">站点列表</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">

        <el-form-item label="线路" prop="subway_no" required>
          <el-input v-model.trim="dialogForm.subway_no" clearable placeholder="请输入线路"/>
        </el-form-item>
        <el-form-item label="区域" prop="region" required>
          <elui-china-area-dht :leave="2" v-model="dialogForm.region" @change="handleRegionChange"></elui-china-area-dht>
        </el-form-item>
        <el-form-item label="是否停用" prop="is_stop">
          <el-switch v-model="dialogForm.is_stop" inline-prompt size="large" :active-value="1"
                     :inactive-value="2" active-text="是" inactive-text="否" />
        </el-form-item>

        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm2(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import chinaAreaData from "china-area-data";

const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

dialogForm.province = '';
dialogForm.province_code = '';
dialogForm.city = '';
dialogForm.city_code = '';
dialogForm.subway_no = '';
dialogForm.is_stop = 2;
dialogForm.region = [];

apiPath.value = 'admin/subway/index';
dialogParams.addApiPath = 'admin/subway/create';
dialogParams.editApiPath = 'admin/subway/update';

onMounted(() => {
  getData()
})

const dialogFormRules = reactive({
  subway_no:[{
    required: true,
    message: "请输入线路",
    trigger: "blur"
  }],
  region: [{
    required: true,
    message: "请选择省市",
    trigger: "change"
  }],
})
const handleEdit = (row,data) => {
  dialogShow.value = true
  dialogParams.type = 'edit'
  dialogParams.data = data
  setTimeout(() => {
    for (let key in dialogForm) {
      dialogForm[key] = row[key]
    }
    dialogForm.region = [];
    dialogForm.region.push(row.province_code.toString())
    dialogForm.region.push(row.city_code.toString())
  }, 50)
}
const handleRegionChange = (e) => {
  dialogForm.province = chinaAreaData['86'][e[0]]
  dialogForm.province_code = e[0]
  dialogForm.city = chinaAreaData[e[0]][e[1]]
  dialogForm.city_code = e[1]
}
const searchRegion = reactive([])
const handleSearchRegionChange =(e)=>{
  if(!e){
    searchParams.province_code = ''
    searchParams.city_code = ''
  }else{
    searchParams.province_code = e[0]
    searchParams.city_code = e[1]
  }
}
const submitDialogForm2 = (formEl) => {
  let params = JSON.parse(JSON.stringify(dialogForm));
  delete params.region
  submitDialogForm(formEl, params)
}
const handleDelete=(row)=> {
  handleAction('admin/subway/delete', {
    subway_id: row.subway_id,
  })
}

const handleStation = (row) => {
  router.push({
    path: "/subwayStation/subwayStation",
    query: {
      subway_id: row.subway_id,
      metaTitle: `【${row.province}-${row.city}-${row.subway_no}号线】 地铁站点`
    },
  })
}


</script>

<style lang="less">

</style>